Page({
    data:{
      product:{},
      star:0,
      content:'',
      pics:[]
    },
  
    onLoad(opt){
      const products = wx.getStorageSync('products')
      const product = products.find(p=>p.id==opt.id)
      this.setData({product})
      wx.setNavigationBarTitle({title:`评价 ${product.title}`})
    },
  
    setStar(e){
      this.setData({star:e.currentTarget.dataset.idx+1})
    },
    onContent(e){this.setData({content:e.detail.value})},
    chooseImage(){
      wx.chooseImage({
        count:3-this.data.pics.length,
        success:res=>this.setData({pics:this.data.pics.concat(res.tempFilePaths)})
      })
    },
    delPic(e){
      const idx=e.currentTarget.dataset.idx
      const pics=this.data.pics
      pics.splice(idx,1)
      this.setData({pics})
    },
  
    submit(){
      if(!this.data.content.trim()){
        wx.showToast({title:'请输入评价内容',icon:'none'})
        return
      }
      const products = wx.getStorageSync('products')
      const idx = products.findIndex(p=>p.id===this.data.product.id)
      products[idx].comment = {
        nick:'微信用户',
        avatar:'/images/club-logo.jpeg',
        star:this.data.star,
        content:this.data.content,
        pics:this.data.pics,
        likes:0,
        liked:false,
        replies:[]
      }
      wx.setStorageSync('products',products)
      wx.showModal({
        title:'提示',
        content:'评价成功，积分 +20',
        showCancel:false,
        success:()=>{
          wx.navigateBack();   // 返回首页
        }
      })
    }
    
  })